package com.niodata.dp.expression.function;

import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.function.FunctionUtils;
import com.googlecode.aviator.runtime.type.AviatorObject;
import com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class RangeNumFunction extends AbstractFunction {

  @Override
  public String getName() {
    return "range_num";
  }

  @Override
  public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2,
        AviatorObject arg3, AviatorObject arg4) {
    String format = FunctionUtils.getStringValue(arg1, env);
    Number start = FunctionUtils.getNumberValue(arg2, env);
    Number end = FunctionUtils.getNumberValue(arg3, env);
    Number step = FunctionUtils.getNumberValue(arg4, env);
    List<String> result = new ArrayList<>(32);
    for (int i = start.intValue(); i < end.intValue(); i += step.intValue()) {
      result.add(String.format(format, i));
    }
    return AviatorRuntimeJavaType.valueOf(result);
  }
}
